package com.itheima.web.controller.sms;

import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.SmsHomeBrandService;
import com.itheima.vo.sms.SmsHomeBrand;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/brand")
@Slf4j
public class SmsHomeBrandController {

    @DubboReference
    private SmsHomeBrandService smsHomeBrandService;

    /**
     * 添加首页推荐品牌
     */
    @PostMapping("/create")
    public ResultData creat(@RequestBody SmsHomeBrand smsHomeBrand) {
         smsHomeBrandService.create(smsHomeBrand);
        return ResultData.ok("操作成功", "");
    }

    /**
     * 批量删除推荐品牌
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam("ids") List<Long> ids){
        boolean res = smsHomeBrandService.removeBatchByIds(ids);
        if (res) {
            return ResultData.ok("操作成功", "");
        } else {
            return ResultData.error("删除失败");
        }

    }

    /**
     * 分页查询推荐品牌
     */

    @GetMapping("/list")
    public ResultData list(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        PageResult pageResult = smsHomeBrandService.selectByPage(brandName, pageNum, pageSize, recommendStatus);
        return ResultData.ok("", pageResult);
    }

    /**
     * 批量修改品牌状态
     */
    @PostMapping("/update/recommendStatus")
    public ResultData updateStatus (@RequestParam("ids") List<Long> ids,@RequestParam("recommendStatus") Integer recommendStatus){
        smsHomeBrandService.updateStatus(ids,recommendStatus);
        return ResultData.ok("修改成功","");
    }

    /**
     * 修改推荐品牌顺序
     */
    @PostMapping("update/sort/{id}")
    public ResultData updateSort(@PathVariable("id") Long id_, @RequestParam("id") Long id, @RequestParam("sort") Integer sort) {
        smsHomeBrandService.updateSort(id, sort);
        return ResultData.ok("修改成功", "");
    }
}
